﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;

namespace WebSys.UI.Control.ZhengShou
{
    public class LoginControl : MPBasicTemplate
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            HttpContext.Current.Session["UserID"] = null;
            HttpContext.Current.Session["UserName"] = null;
            HttpContext.Current.Session["LoginUserName"] = null;
            ImageButton login = (ImageButton)FindControl("login");
            login.Click += new ImageClickEventHandler(login_Click);
        }
        protected void login_Click(object sender, EventArgs e)
        {
            TextBox UserName = (TextBox)FindControl("UserName");
            TextBox Password = (TextBox)FindControl("Password");
            EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
            User user = _EluZSDataProvider.GetUser(UserName.Text, Password.Text);

            if (string.IsNullOrEmpty(user.Id))
            {
                HttpContext.Current.Response.Write("<script>alert('用户名密码错误！');</script>");
            }
            else
            {
                if (user.Loginid != "admin")
                {
                    EluZSProvider.Rizhi rizhi = new EluZSProvider.Rizhi();
                    rizhi.Eventname = "系统登录";
                    rizhi.Eventuser = user.Id;
                    bool save = _EluZSDataProvider.SaveRizhi(rizhi);
                }
                HttpContext.Current.Session["UserID"] = user.Id;
                HttpContext.Current.Session["UserName"] = user.Loginid;
                HttpContext.Current.Session["LoginUserName"] = user.Name;
                HttpContext.Current.Response.Redirect("Index.aspx");
            }

        }
        public string getRadom()
        {
            Random randomGenerator = new Random(DateTime.Now.Millisecond);

            String RandData = "";
            for (int i = 0; i < 20; i++)
            {
                RandData += Convert.ToChar(randomGenerator.Next(97, 122));
            }

            return RandData;
        }
        public string fun_MD5(string str)
        {
            byte[] b = System.Text.Encoding.GetEncoding(1252).GetBytes(str);
            b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');
            return ret;
        }
        public Byte[] hexstr2array(string HexStr)
        {
            string HEX = "0123456789ABCDEF";
            string str = HexStr.ToUpper();
            int len = str.Length;
            byte[] RetByte = new byte[len / 2];
            for (int i = 0; i < len / 2; i++)
            {
                int NumHigh = HEX.IndexOf(str[i * 2]);
                int NumLow = HEX.IndexOf(str[i * 2 + 1]);

                RetByte[i] = Convert.ToByte(NumHigh * 16 + NumLow);
            }
            return RetByte;
        }
        public string getservicestr(string userpwd)
        {

            //these for MD5_HMAC
            string ipad = "";
            string opad = "";

            {
                for (int i = 0; i < 64; i++)
                {
                    ipad += "6";
                    opad += "\\";
                }
            }

            string Password = userpwd;

            int KLen = Password.Length;
            string iResult = "";
            {
                for (int i = 0; i < 64; i++)
                {
                    if (i < KLen)
                        iResult += Convert.ToChar(ipad[i] ^ Password[i]);
                    else
                        iResult += Convert.ToChar(ipad[i]);
                }
            }
            iResult += HttpContext.Current.Session["radom"].ToString();
            iResult = fun_MD5(iResult);

            byte[] Test = hexstr2array(iResult);

            iResult = "";

            char[] b = System.Text.Encoding.GetEncoding(1252).GetChars(Test);

            for (int i = 0; i < b.Length; i++)
            {
                iResult += b[i];
            }

            string oResult = "";
            {
                for (int i = 0; i < 64; i++)
                {
                    if (i < KLen)
                        oResult += Convert.ToChar(opad[i] ^ Password[i]);
                    else
                        oResult += Convert.ToChar(opad[i]);
                }
            }

            oResult += iResult;

            string Result = fun_MD5(oResult).ToUpper();
            return Result;
        }
    }
}


